﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LunchListCore.Entities;
using LunchListCore.Interfaces.Agents;
using LunchListCore.Interfaces.Models;

namespace LunchListAgents
{
    public class GuestAgent : IGuestAgent
    {
        protected readonly LunchListEntities _context;

        public GuestAgent(LunchListEntities context)
        {
            _context = context;
        }

        public IGuest GetOrCreateGuest(String adLogin, String name)
        {
           IGuest guest = _context.Guests.FirstOrDefault(x => x.AdUserId == adLogin);
            if (guest == default(IGuest))
            {
                guest = _context.Guests.Add(new Guest()
                {

                    Name = name,
                    AdUserId = adLogin,
                    CreatedDateTime = DateTime.Now,
                    ModifiedDateTime = DateTime.Now,
                    CreatedUserId = adLogin,
                    ModifiedUserId = adLogin
                });

                _context.SaveChanges();
            }
            return guest;
        }
    }
}
